package com.houbank.demo.rabbitmq.utils;

import com.alibaba.fastjson.JSON;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.AbstractMessageConverter;
import org.springframework.amqp.support.converter.MessageConversionException;

import java.io.UnsupportedEncodingException;

/**
 * Created by zhaoqiguo on 2016/9/21.
 */
public class FastJsonMessageConverter extends AbstractMessageConverter {


    private static Log log = LogFactory.getLog(FastJsonMessageConverter.class);

    public static final String DEFAULT_CHARSET = "UTF-8";

    private volatile String defaultCharset = DEFAULT_CHARSET;

    public FastJsonMessageConverter() {
        super();
        //init();
    }

    public void setDefaultCharset(String defaultCharset){
        this.defaultCharset = (defaultCharset !=null) ? defaultCharset:DEFAULT_CHARSET;
    }

  @Override
    protected Message createMessage(Object objectToConvert, MessageProperties messageProperties)throws MessageConversionException {
      Object bytes = null;

      byte[] bytes1;
      try {
          String jsonString = JSON.toJSONString(objectToConvert);
          bytes1 = jsonString.getBytes(this.defaultCharset);
      } catch (UnsupportedEncodingException var5) {
          throw new MessageConversionException("Failed to convert Message content", var5);
      }

      messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
      messageProperties.setContentEncoding(this.defaultCharset);
      if(bytes1 != null) {
          messageProperties.setContentLength((long)bytes1.length);
      }
      return new Message(bytes1, messageProperties);
    }

    @Override
    public Object fromMessage(Message message) throws MessageConversionException {
        return null;
    }

    public <T> T fromMessage(Message message,T t) {
        String json = "";
        try {json = new String(message.getBody(),defaultCharset);
        } catch (UnsupportedEncodingException e) {e.printStackTrace();
        }
        return (T) JSON.parseObject(json, t.getClass());
    }

  /*  publivc <T> T fromMessage (Message message,T t){
        String json = "";
        try{
            json = new String(message.getBody(),defaultCharset);
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        return (T)JsonMapper.fromJson(json,);
    }*/




}
